#!/usr/bin/env node

var buf1 = new Buffer(256);
buf1[0]=0;//初始化第一个字节为0

console.log("buf1 length:",buf1.length);
console.log("buf1 content:",buf1);

//初始化每个字节比上一个大一
for(var i=0;i<buf1.length;i++) buf1[i]=i; 
console.log(buf1);

var buf2=buf1.slice(-10);//或者(246,256)
console.log("\nbuf2 length:",buf2.length);
console.log("buf2 content:",buf2);

buf2.fill(0);//fill()方法初始化
console.log("\nbuf2 content:",buf2);

//用数组实例化Buffer
var array=['h',0x23,7,0xba];
var buf3=new Buffer(array);
console.log("\nbuf3 length:",buf3.length);
console.log("buf3 content:",buf3);//显示十六进制值


//用字符串实例化Buffer
var buf4=new Buffer('hello world','utf8');
console.log('\nbuf4 length:',buf4.length);
console.log('buf4 content:',buf4.toString());

//用buffer实例化buffer
var buf=new Buffer(buf4.length);
buf4.copy(buf,0,0,buf4.length);
console.log("\nbuf content:",buf.toString());

//用buf4的内容复制到buf3中
buf4.copy(buf3,0,0,buf4.length);
console.log("\nbuf3 length:",buf3.length);
console.log("buf3 content:",buf3.toString());

